<?php
require "getConnect.php";
//获取公众号相关信息
$chatSql = "select appId,appSecret,urlSite from lf_wechat_setting";
$chatSqlList = getConnect::getConnectData($chatSql);
$appId = $chatSqlList[0]["appId"];
$appSecret = $chatSqlList[0]["appSecret"];
$webUrl = $chatSqlList[0]["urlSite"];
$openId = $_GET["openId"];
if (isset($_GET["code"])) {
    $code = $_GET["code"];
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
    $res = json_decode(httpGet($url));
    $access_token = $res->access_token;
    $_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openId&lang=zh_CN ";
    $_res = json_decode(httpGet($_url));
    echo $_res ;
//    $openid = $_res->openid;
//    $locationUrl = "http://www.teamispower.com/vote/#/my?paraId=" . $paraId . "&openId=" . $openid;
//    header("Location:$locationUrl");
} else {
    $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appId&redirect_uri=http%3a%2f%2f$webUrl%2fapi%2fmobile%2fgetSingleOpenId.php%3fopenId=$openId&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
    header("Location:$url");
}

/**
 * @param $url
 * @return mixed
 *
 * 发送httpGet请求
 */
function httpGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 2);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}